const httpUrl = getApp().globalData.httpUrl;

const hideLoading = () =>{
  return new Promise((resolve, reject) => {
    wx.hideLoading({
      success: () =>{
        resolve(true); // 成功关闭loading，返回true
      },
      fail: () =>{
        reject(false); // 关闭loading失败，返回false
      }
    });
  });
};
Page({

  /**
   * 页面的初始数据
   */
  data: {
    OrderList:[],
  },
  refund(e){
    var that = this;
    wx.showModal({
      title: '申请退款',
      content: '您的货物尚未发货，可直接申请退款',
      success(res) {
        if (res.confirm) {
          
    wx.request({
      url: httpUrl + '/order/refund',
      method:'PUT',
      header:{
        'Shop_token':wx.getStorageSync('Shop_token')
      },
      data:{
        uid:e.currentTarget.dataset.uid
      },
      success(res){
        if (res.data.code == 20031) {
          that.getOrderList();
          wx.showToast({
            title: res.data.msg,
            icon:'none',
            duration:1000
          })
        }
        else if (res.data.code == 401) {
          console.log(res.data.msg);
          wx.removeStorageSync('Shop_token');
          wx.navigateTo({
            url: '/pages/login/login',
          })
        }
        else if (res.data.code == 402) {
          console.log(res.data.msg);
          // wx.removeStorageSync('Shop_token');
          wx.navigateTo({
            url: '/pages/login/login',
          })
        }
        else{
          console.log(res.data.msg);
        }
      },
      fail(err){
        console.log(err);
      }
    })
          // 在这里执行用户确认后的操作
        } else if (res.cancel) {
          console.log('用户点击了取消按钮');
          // 在这里执行用户取消操作
        }
      }
    })
    
  },
  shouhuo(e){
    var that = this;
    wx.showModal({
      title: '确认收货',
      content: ' 请确保已经收到货物',
      success(res) {
        if (res.confirm) {       
    wx.request({
      url: httpUrl + '/order/update',
      method:'PUT',
      header:{
        'Shop_token':wx.getStorageSync('Shop_token')
      },
      data:{
        uid:e.currentTarget.dataset.uid,
        status:3,
      },
      success(res){
        if (res.data.code == 20031) {
          that.getOrderList();
          wx.showToast({
            title: "收货成功",
            icon:'success',
            duration:1000
          })
        }
        else if (res.data.code == 401) {
          console.log(res.data.msg);
          wx.removeStorageSync('Shop_token');
          wx.navigateTo({
            url: '/pages/login/login',
          })
        }
        else if (res.data.code == 402) {
          console.log(res.data.msg);
          // wx.removeStorageSync('Shop_token');
          wx.navigateTo({
            url: '/pages/login/login',
          })
        }
        else{
          console.log(res.data.msg);
        }
      },
      fail(err){
        console.log(err);
      }
    })
          // 在这里执行用户确认后的操作
        } else if (res.cancel) {
          console.log('用户点击了取消按钮');
          // 在这里执行用户取消操作
        }
      }
    })
  },

  comment(e){
    wx.navigateTo({
      url: '/pages/comment/comment?shopId=' + e.currentTarget.dataset.item.shopId + '&order=' + e.currentTarget.dataset.item.uid,
    })
  },


  getOrderList(){
    var that = this;
    wx.request({
      url: httpUrl + '/order' ,
      method:'GET',
      header:{
        'Shop_token':wx.getStorageSync('Shop_token')
      },
      success(res){
        if (res.data.code == 20041) {
          that.setData({
            OrderList:res.data.data.rows
          })
        }
        else if (res.data.code == 401) {
          console.log(res.data.msg);
          wx.removeStorageSync('Shop_token');
          wx.navigateTo({
            url: '/pages/login/login',
          })
        }
        else if (res.data.code == 402) {
          console.log(res.data.msg);
          // wx.removeStorageSync('Shop_token');
          wx.navigateTo({
            url: '/pages/login/login',
          })
        }
        else{
          console.log(res.data.msg);
        }
      },
      fail(err){
        console.log(err);
      }
    })
  },
  gotocareful(e){
    wx.navigateTo({
      url: '/pages/careful/careful?uid=' + e.currentTarget.dataset.uid,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getOrderList();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})